package com.uday.kraterchat.chatclient.business
{
	import com.adobe.cairngorm.business.ServiceLocator;
	import com.uday.frmwrk.data.BaseRequestItem;
	import com.uday.kraterchat.chatclient.data.LoginInfoItem;
	import com.uday.kraterchat.chatclient.data.UserInfoBaseItem;
	import com.uday.kraterchat.chatclient.data.UserInfoDetItem;
	
	import mx.messaging.Consumer;
	import mx.messaging.events.MessageEvent;
	import mx.messaging.events.MessageFaultEvent;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.remoting.RemoteObject;
	
	public class UserServiceDelegate
	{
		private var objResponder:IResponder;
		private var objRemoteSrvc:RemoteObject;
		private var objChatListConsumer:Consumer;

		public function UserServiceDelegate(objResponder:IResponder)
		{
			this.objResponder = objResponder;
			objRemoteSrvc = ServiceLocator.getInstance().getRemoteObject("loginService");
			objChatListConsumer = ServiceLocator.getInstance().getConsumer("subMsgs");
		}
		
		public function loginUser(objLoginInfoItem:LoginInfoItem):void{
			var objRequestItem:BaseRequestItem = new BaseRequestItem();
			objRequestItem.objReqData = objLoginInfoItem;
			objRequestItem.strActivityMap = "loginUser";
			
			var token : AsyncToken = objRemoteSrvc.execute( objRequestItem );
			token.addResponder(objResponder);
		}

		public function registerNewUser(objUserInfoItem:UserInfoDetItem):void{
			var objRequestItem:BaseRequestItem = new BaseRequestItem();
			objRequestItem.objReqData = objUserInfoItem;
			objRequestItem.strActivityMap = "registerUser";
			
			var token : AsyncToken = objRemoteSrvc.execute( objRequestItem );
			token.addResponder(objResponder);
		}
		
		public function subsribeMessages(strDestination:String):void{
			objChatListConsumer.destination = strDestination;
			objChatListConsumer.addEventListener(MessageEvent.MESSAGE,objResponder.result);
			objChatListConsumer.addEventListener(MessageFaultEvent.FAULT,objResponder.fault);
			objChatListConsumer.subscribe();
		}
		
		public function keepAlive(objUserInfoBaseItem:UserInfoBaseItem):void{
			var objRequestItem:BaseRequestItem = new BaseRequestItem();
			objRequestItem.objReqData = objUserInfoBaseItem;
			objRequestItem.strActivityMap = "keepAlive";
			
			var token : AsyncToken = objRemoteSrvc.execute( objRequestItem );
			token.addResponder(objResponder);
		}
	}
}